#include "demo_room_shell.h"
#include "msgids.h"
#include "pageinfogetter.hpp"
#include "valueRange.hpp"
#include "automessage.h"

demo_room_logic_shell::demo_room_logic_shell() :
    m_car(0),
    m_pause_car(false){
    m_rotateSpeed = Utility::page_info_getter::Instance().
        get("demo_room", "car", "rotate_speed", 1.0) ;
    
}

void demo_room_logic_shell::on_add_client(HCLIENT hclient) {

    send_demo_camera_position_rotation(hclient) ;
}

void demo_room_logic_shell::send_demo_camera_position_rotation(HCLIENT hclient) {
    normal_number_range pos =
        Utility::page_info_getter::Instance().
        get<normal_number_range>("demo_room",
                                 "camera_info",
                                 "position") ;
    LTVector position(pos[0],pos[1],pos[2]) ;
    CAutoMessage msg ;
    msg.Writeuint8(MSG_DEMOROOM_INFO) ;
    msg.WriteLTVector(position) ;
    g_pLTServer->SendToClient(msg.Read(), hclient,
                              NULL) ; // send all 
    
}


void demo_room_logic_shell::respond_pause_car_message(server_message_param_type param) {
    
}

void demo_room_logic_shell::update(float time_inteval) {
    if(m_car && !m_pause_car) {
        LTRotation r ;
        g_pLTServer->GetObjectRotation(m_car->GetHOBJECT(), &r) ;
        r.Rotate(r.Up(), time_inteval * m_rotateSpeed) ;
         g_pLTServer->SetObjectRotation(m_car->GetHOBJECT(), &r) ;
    }
    send_all_binding_infos() ;
}

LPBASECLASS demo_room_logic_shell::on_client_enter_world(HCLIENT hclient) {
   
    std::string car_name = Utility::page_info_getter::Instance().get<std::string>
        ("car_infos", "demo_info", "use_name") ;
    m_car = create_jupiter_car(car_name.c_str()) ;
    
    if(!m_car) g_pLTServer->CPrint("create client object failed") ;
    if(m_car) {
        send_create_binding_shader_message(hclient, m_car, car_name.c_str()) ;
        g_pLTServer->SetClientUserData(hclient, m_car) ;
    }
    return m_car ;
}

void demo_room_logic_shell::on_object_message(LPBASECLASS pSender,
                                              uint32 messageID,
                                              ILTMessage_Read* pMsg) {
}

void demo_room_logic_shell::on_message(HCLIENT hSender,
                                       ILTMessage_Read* pMsg) {
    pMsg->SeekTo(0) ;
    uint8 messageID = pMsg->Readuint8() ;
    g_pLTServer->CPrint("the key is %d", messageID) ;
    if(messageID == MSG_SEND_KEY_PRESS) {
        m_pause_car = !m_pause_car ;
    }
}



